package presentacion;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JTable;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.JTextPane;

import negocio.DirectorEstrategia;
import negocio.DirectorTarea;
import negocio.DirectorUsuario;

import negocio.TransferDirector;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

public class DirectorPanelMarketingTareas extends JPanel implements DirectorPanel {
	private JTable table;
	private JButton btnDenegar;
	private JTextPane textPane;
	

	private JLabel lblNuevaTarea;
	private JTextField textField;
	private JLabel lblNombre;
	private JLabel lblPara;
	private JComboBox comboBox;
	private JLabel lblDescripcin;
	private JCheckBox chckbxMostrarSoloLos;

	/**
	 * Create the panel.
	 */
	public DirectorPanelMarketingTareas() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{288, 39, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridheight = 7;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		add(scrollPane, gbc_scrollPane);
		
		table = new JTable();
		
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
			},
			new String[] {
				"Nombre", "Fecha", "Estado", "Descripcion"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(1).setPreferredWidth(70);
		table.getColumnModel().getColumn(1).setMaxWidth(70);
		table.getColumnModel().getColumn(2).setMaxWidth(75);
		table.getColumnModel().getColumn(3).setPreferredWidth(150);
		scrollPane.setViewportView(table);
		
		chckbxMostrarSoloLos = new JCheckBox("<html> Mostrar solo los no terminados </html>");
		chckbxMostrarSoloLos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actualizar();
			}
		});
		GridBagConstraints gbc_chckbxMostrarSoloLos = new GridBagConstraints();
		gbc_chckbxMostrarSoloLos.gridwidth = 2;
		gbc_chckbxMostrarSoloLos.insets = new Insets(0, 0, 5, 5);
		gbc_chckbxMostrarSoloLos.gridx = 1;
		gbc_chckbxMostrarSoloLos.gridy = 0;
		add(chckbxMostrarSoloLos, gbc_chckbxMostrarSoloLos);
		
		lblNuevaTarea = new JLabel("<html><b>Nueva tarea</b></html>");
		GridBagConstraints gbc_lblNuevaTarea = new GridBagConstraints();
		gbc_lblNuevaTarea.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblNuevaTarea.insets = new Insets(0, 0, 5, 5);
		gbc_lblNuevaTarea.gridx = 1;
		gbc_lblNuevaTarea.gridy = 1;
		add(lblNuevaTarea, gbc_lblNuevaTarea);
		
		lblPara = new JLabel("Para:");
		GridBagConstraints gbc_lblPara = new GridBagConstraints();
		gbc_lblPara.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblPara.insets = new Insets(0, 0, 5, 5);
		gbc_lblPara.gridx = 1;
		gbc_lblPara.gridy = 2;
		add(lblPara, gbc_lblPara);
		
		comboBox = new JComboBox();
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.gridwidth = 2;
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 3;
		add(comboBox, gbc_comboBox);
		
		lblNombre = new JLabel("Nombre tarea:");
		GridBagConstraints gbc_lblNombre = new GridBagConstraints();
		gbc_lblNombre.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblNombre.insets = new Insets(0, 0, 5, 5);
		gbc_lblNombre.gridx = 1;
		gbc_lblNombre.gridy = 4;
		add(lblNombre, gbc_lblNombre);
		
		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 0);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 2;
		gbc_textField.gridy = 4;
		add(textField, gbc_textField);
		textField.setColumns(10);
		
		lblDescripcin = new JLabel("Descripci\u00F3n:");
		GridBagConstraints gbc_lblDescripcin = new GridBagConstraints();
		gbc_lblDescripcin.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblDescripcin.insets = new Insets(0, 0, 5, 5);
		gbc_lblDescripcin.gridx = 1;
		gbc_lblDescripcin.gridy = 5;
		add(lblDescripcin, gbc_lblDescripcin);
		
		textPane = new JTextPane();
		GridBagConstraints gbc_textPane = new GridBagConstraints();
		gbc_textPane.insets = new Insets(0, 0, 5, 0);
		gbc_textPane.gridwidth = 2;
		gbc_textPane.fill = GridBagConstraints.BOTH;
		gbc_textPane.gridx = 1;
		gbc_textPane.gridy = 6;
		add(textPane, gbc_textPane);
		
		btnDenegar = new JButton("Enviar");
		btnDenegar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (comboBox.getSelectedItem()!=null){
					DirectorTarea dt= new DirectorTarea(textField.getText(),textPane.getText(),"","",0,false,
							((DirectorUsuario)comboBox.getSelectedItem()).getDni());
					TransferDirector datos=new TransferDirector();
					datos.setNuevaTarea(dt);
					SingletonControlador.getInstancia().evento(Evento.DIRECTOR_INSERTAR_TAREA, datos);
					actualizar();
				}

			}
		});
		
		GridBagConstraints gbc_btnDenegar = new GridBagConstraints();
		gbc_btnDenegar.insets = new Insets(0, 0, 0, 5);
		gbc_btnDenegar.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDenegar.gridx = 1;
		gbc_btnDenegar.gridy = 7;
		add(btnDenegar, gbc_btnDenegar);
		actualizar();

	}
	
	
	
	
	
	
	public void actualizar(){

		
		//cojer solo marketring
		TransferDirector datos=new TransferDirector();
		datos.setTipoListaEmpleados(Roles.Marketing.ordinal());
		SingletonControlador.getInstancia().evento(Evento.DIRECTOR_GET_LISTA_PERSONAL, datos);
		
		comboBox.removeAllItems();
		for(Iterator<DirectorUsuario> it=datos.getListaEmpleado().iterator();it.hasNext();){
			comboBox.addItem(it.next());
		}

		
		datos=new TransferDirector();
		SingletonControlador.getInstancia().evento(Evento.DIRECTOR_GET_LISTA_TAREAS, datos);
		
		//Borrar datos anteriores.
		while(((DefaultTableModel)table.getModel()).getRowCount()!=0){
			((DefaultTableModel)table.getModel()).removeRow(0);
		}
		

		
		for(Iterator<DirectorTarea> it=datos.getListaTareas().iterator();it.hasNext();){
			DirectorTarea dp=it.next();
			String estado= dp.isTerminada()?"Terminada":"En proceso";
			
		if (!chckbxMostrarSoloLos.isSelected()||(dp.isTerminada()&&chckbxMostrarSoloLos.isSelected()))
				((DefaultTableModel)table.getModel()).addRow(new Object[]{dp.getMotivo(),dp.getFecha(),estado,dp.getDescripcion()});
				
			}
		}
		
	}


